﻿<%@ Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" Theme="ThemeRequisition" %>

<script runat="server">
    Dim id_request As Integer = "0"
    Dim requisition1 As requisition = New requisition()
    
    Protected Sub Page_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs)
        
        If IsPostBack Then
            Response.Write("Postback")
        End If
        
        Dim connection_string As String = ConfigurationManager.ConnectionStrings("conn").ConnectionString

        Dim SqlConnection1 As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection()
        SqlConnection1.ConnectionString = connection_string
        SqlConnection1.Open()

        Dim SqlCommand1 As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand()
        'Dim string_sql As String = "INSERT INTO requisitions(id_item,id_user,date_rec,delivered) VALUES(1,1,GETDATE(),GETDATE());"
        Dim string_sql As String = "Select * from requisitions"
        SqlCommand1.Connection = SqlConnection1
        SqlCommand1.CommandText = string_sql
        GridView1.DataKeyNames= New String() {"id_requisition"}
        GridView1.DataSource = SqlCommand1.ExecuteReader()
        GridView1.DataBind()
        SqlConnection1.Close()
    End Sub
    
    Public Sub formfieldtovariable() 'coloca os campos da variavel para o form
        requisition1.id_requisition = Integer.Parse(txt_id_requisition.Text)
        requisition1.id_item = Integer.Parse(txt_id_item.Text)
        requisition1.id_user = Integer.Parse(txt_id_user.Text)
        requisition1.date_rec = Date.Parse(txt_date_rec.Text)
        requisition1.delivered = Date.Parse(txt_delivered.Text)
    End Sub
    
    Public Sub show_requisition_on_page()
        txt_date_rec.Text = requisition1.date_rec.ToString()
        txt_delivered.Text = requisition1.delivered.ToString()
        txt_id_item.Text =  requisition1.id_item.ToString()
        txt_id_requisition.Text = requisition1.id_requisition.ToString()
        txt_id_user.Text = requisition1.id_user.ToString()
      
    End Sub
    
    Public Sub cleanfields()
        txt_date_rec.Text = ""
        txt_delivered.Text = ""
        txt_id_item.Text = ""
        txt_id_requisition.Text = ""
        txt_id_user.Text = ""
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        show_requisition_on_page()
    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    cleanfields()
    End Sub

Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        
        requisition1.id_item = txt_id_item.Text
        requisition1.id_user = txt_id_user.Text
        requisition1.date_rec = txt_date_rec.Text
        requisition1.delivered = txt_delivered.Text
        
      
        
        lbl_info.Text = "Success"+ requisition1.insert()
        lbl_info.ForeColor = Drawing.Color.Orange
        
End Sub

    Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        requisition1.id_requisition = txt_id_requisition.Text
        requisition1.id_item = txt_id_item.Text
        requisition1.id_user = txt_id_user.Text
        requisition1.date_rec = txt_date_rec.Text
        requisition1.delivered = txt_delivered.Text
           
        
        lbl_info.Text = "Success" + requisition1.update()
        lbl_info.ForeColor = Drawing.Color.Orange
    End Sub

    Protected Sub Button8_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        
        lbl_info.Text = "Success" + requisition1.first()
        lbl_info.ForeColor = Drawing.Color.Orange
        
        txt_id_requisition.Text = requisition1.id_requisition
        txt_id_item.Text = requisition1.id_item
        txt_id_user.Text = requisition1.id_user
        txt_date_rec.Text = requisition1.date_rec
        txt_delivered.Text = requisition1.delivered
           
        
        
    End Sub

    Protected Sub Button9_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        lbl_info.Text = "Success" + requisition1.last()
        lbl_info.ForeColor = Drawing.Color.Orange
        
        txt_id_requisition.Text = requisition1.id_requisition
        txt_id_item.Text = requisition1.id_item
        txt_id_user.Text = requisition1.id_user
        txt_date_rec.Text = requisition1.date_rec
        txt_delivered.Text = requisition1.delivered
    End Sub
    
    Protected Sub btn_next_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        formfieldtovariable()
        lbl_info.Text = "Success" + requisition1.nextt()
        lbl_info.ForeColor = Drawing.Color.Orange
        
        txt_id_requisition.Text = requisition1.id_requisition
        txt_id_item.Text = requisition1.id_item
        txt_id_user.Text = requisition1.id_user
        txt_date_rec.Text = requisition1.date_rec
        txt_delivered.Text = requisition1.delivered
    End Sub

    Protected Sub btn_previous_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        
        lbl_info.Text = "Success" + requisition1.previous(txt_id_requisition.Text)
        lbl_info.ForeColor = Drawing.Color.Orange
        
        txt_id_requisition.Text = requisition1.id_requisition
        txt_id_item.Text = requisition1.id_item
        txt_id_user.Text = requisition1.id_user
        txt_date_rec.Text = requisition1.date_rec
        txt_delivered.Text = requisition1.delivered
    End Sub

    Protected Sub btn_delete_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        formfieldtovariable()
        lbl_info.Text = "Success! " + requisition1.delete()
        lbl_info.ForeColor = Drawing.Color.Orange
        cleanfields()
        
    End Sub

    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Page.ClientScript.RegisterStartupScript("".GetType(), "ClientScript", "alert('" + GridView1.SelectedValue.ToString() + "');", True)
        'requisition1.id_requisition = Integer.Parse(GridView1.SelectedValue.ToString())
        txt_id_requisition.Text = GridView1.SelectedRow.Cells(1).Text
        txt_id_item.Text = GridView1.SelectedRow.Cells(2).Text
        txt_id_user.Text = GridView1.SelectedRow.Cells(3).Text
        txt_date_rec.Text = GridView1.SelectedRow.Cells(4).Text
        txt_delivered.Text = GridView1.SelectedRow.Cells(5).Text
    End Sub

    Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs)
        'delete
         Page.ClientScript.RegisterStartupScript("".GetType(), "ClientScript", "alert('id:" + GridView1.Rows(e.RowIndex).Cells(2).Text + "');", True)
        requisition1.id_requisition = Integer.Parse(GridView1.Rows(e.RowIndex).Cells(2).Text)
        requisition1.delete()
        cleanfields()
        
    End Sub
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    <style type="text/css">
    </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <h1 class="style1">Requisition</h1>
    <table width="100%">
        <tr>
            <td>
                <asp:Label ID="lbl_id_requisition" runat="server" Text="id_requisition"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txt_id_requisition" runat="server" ReadOnly="true" Text=0  ></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="lbl_id_item" runat="server" Text="id_item"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txt_id_item" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="lbl_id_user" runat="server" Text="id_user"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txt_id_user" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="lbl_date_rec" runat="server" Text="date_rec"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txt_date_rec" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="lbl_delivered" runat="server" Text="delivered"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txt_delivered" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Button ID="btn_first" runat="server" Text="First" onclick="Button8_Click" />
                <asp:Button ID="btn_next" runat="server" Text="Next" onclick="btn_next_Click" />
                <asp:Button ID="btn_delete" runat="server" Text="Delete" 
                    onclick="btn_delete_Click" />
                <asp:Button ID="btn_previous" runat="server" Text="Previous" 
                    onclick="btn_previous_Click" />
                <asp:Button ID="btn_last" runat="server" Text="Last" onclick="Button9_Click" />
            </td>
            <td>
                <asp:Button ID="btn_show" runat="server" Text="Show" onclick="Button1_Click" />
                <asp:Button ID="btn_clear" runat="server" Text="Clear" onclick="Button2_Click"/>
                <asp:Button ID="btn_insert" runat="server" Text="Insert" onclick="Button3_Click"/>
                <asp:Button ID="btn_update" runat="server" onclick="Button4_Click" Text="Update" />
                
                <asp:Label ID="lbl_info" runat="server"></asp:Label>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                
                <asp:GridView ID="GridView1" runat="server" 
                    onselectedindexchanged="GridView1_SelectedIndexChanged" 
                    onrowdeleting="GridView1_RowDeleting">
                    <Columns>
                        <asp:CommandField HeaderText="Select" ShowHeader="True" 
                            ShowSelectButton="True" />
                        <asp:ButtonField ButtonType="Button" CommandName="Delete" HeaderText="Delete" 
                            ShowHeader="True" Text="Delete" />
                    </Columns>
                </asp:GridView>
                
            </td>
            
        </tr>
    </table>
</asp:Content>

